package dao

import (
	"fanqiechaodan-blog/models"
	"go.uber.org/zap"
)

func GetAllCategory() ([]models.Category, error) {
	var res []models.Category
	err := DB.SelectList("SELECT * FROM blog_category", &res)
	return res, err
}

func GetCategoryNameById(cid int) (string, error) {
	sql := "SELECT name from blog_category WHERE cid = ?"
	row := DB.QueryRow(sql, cid)
	if row.Err() != nil {
		zap.S().Errorf("执行sql失败;sql:%s,cid:%s,err:%s", sql, cid, row.Err().Error())
		return "", row.Err()
	}
	var res string
	err := row.Scan(&res)
	if err != nil {
		zap.S().Errorf("Scan值失败;sql:%s,cid:%s,err:%s", sql, cid, err.Error())
		return "", row.Err()
	}
	return res, nil
}
